package com.yanqu.road.server.cmd.activity.cross;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.StrongholdActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.GeneralActivityRankMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_GENERAL_ACTIVITY_MEMBER_CONDITION_DATA, desc = "获取商会成员贡献值")
public class GeneralActivityMemberConditionDataCmd extends AbstractCommand {

    private int activityId;

    private String unionUid;

    private int type;
    private long serverId;
    private boolean sendResp;
    private UnionActivityConditionData unionActivityConditionData;
    private StrongholdActivityProto.StrongholdWarActivityConditionUnionMemberDataReqMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = StrongholdActivityProto.StrongholdWarActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        unionUid = reqMsg.getUnionUid();
        type = reqMsg.getType();
        serverId = reqMsg.getServerId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if (null == activityInfo) {
            sendResp = true;
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        // 商会分组活动走这边
        if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
            YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
            builder.setActivityId(activityInfo.getActivityId());
            builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
            builder.setIdKey(String.valueOf(type));
            builder.setSeeUnionUid(unionUid);
            player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_CONTRIBUTION, builder, activityId);
            return;
        }

        if (false) {
            sendResp = true;
            unionActivityConditionData = GeneralActivityRankMgr.getUnionActivityConditionData(activityId, type, unionUid);
            if (null == unionActivityConditionData) {
                ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
                return;
            }
        } else {
            CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.newBuilder();
            msg.setActivityId(activityId);
            msg.setUnionUid(unionUid);
            msg.setServerId(serverId);
            msg.setType(type);
            msg.setRankType(2);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_MEMBER_VALUE, msg);
            GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (sendResp) {
            StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.Builder respMsg = StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.newBuilder();
            respMsg.setRet(ret);
            if (0 == ret) {
                if (null != unionActivityConditionData) {
                    ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = ActivityPb.parseActivityConditionUnionMemberDataListMsg(
                            UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, GeneralActivityRankMgr.getUserActivityConditionDataMap(activityId, type)));
                    respMsg.setDataList(dataMsg);
                }
            }
            player.sendPacket(Protocol.U_GENERAL_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
        }
    }

}
